package chatroom;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 *客户端输入流的线程
 * @Author: xxf
 * @CreateDate: 2020/9/4 19:11
 * 1.构造方法传参获取socket对象
 * 2.读取服务器端消息
 */
public class ReadThread extends Thread{

    /**
     * 字符输入流
     */

    BufferedReader reader;


    /**
     * 通过构造方法将用户socket对象伟过来,调用socket.getInputStream()读取服务器发送消息
     *
     * @param socket1
     */
    public ReadThread(Socket socket1) {
        try {
            reader =new BufferedReader(new InputStreamReader(socket1.getInputStream()));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
       try {
          //一直循环读取
          while (true){
              //读取服务器发送的消息
              String receiveMess= reader.readLine();
              //输出消息
              System.out.println(receiveMess);
          }
       }catch (Exception e){
           e.printStackTrace();
       }
    }
}
